
//#include <pic.h>
#include "main.h"


//unsigned char PortSelect = 0;

void putch ( unsigned char ch, unsigned char Port )
{
	unsigned long CTS_TimeOut = 7000000; /* 12-13 Sec */

	while ( CTS != 0 )
	{
		if ( --CTS_TimeOut == 0 )
		{
			ModemStatus = MODEM_NOT_ANSWER;
			//RESET();
		}
	}

	if ( Port == 1 )
	{
		while( !TRMT )	/* set when register is empty */
			continue;
		TXREG1 = ch;
	}
	else
	{
		while( !TRMT2 )	/* set when register is empty */
			continue;
		TXREG2 = ch;
	}
}

void putsf( const unsigned char *s, unsigned char Port )
{
	while ( *s )	
		putch(*s++, Port);
}

void puts ( unsigned char *s, unsigned char Port )
{
	while ( *s )	
		putch(*s++, Port );
}

void putds (unsigned char *s, unsigned int length, unsigned char Port )		// put a string with define string length
{
	while ( length-- )
	{
		putch ( *s++, Port );
	}
}


unsigned char getch( unsigned char Port )
{
	if( Port == 1 )
	{
		while( !RC1IF )
		{
			if ( OERR )					  /* ERROR - input buffer is overflow */
			{
				CREN = 0;
				NOP();
				CREN = 1;
			}
		}	
		return RCREG1;
	}
	else
	{
		while( !RC2IF )
		{
			if ( OERR2 )					  /* ERROR - input buffer is overflow */
			{
				CREN2 = 0;
				NOP();
				CREN2 = 1;
			}
		}
		return RCREG2;
	}
}

unsigned char getche( unsigned char Port )
{
	unsigned char buff;

	buff = getch(Port);
	putch (buff, Port);

	return buff;	
}
